package apis.amapv2.com.tbs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    private static String[] titles = null;//选项对应的标题
    private int[] mIconResourse;//选项对应的图标
    private ArrayList<HashMap<String, Object>> items = new ArrayList<>();//GridView的数据源
    private static final int TBS_WEB = 0;//加载网页
    private static final int FULL_SCREEN_VIDEO = 1;//播放视屏
    private static final int FILE_CHOOSER = 2;//选择文件

    private Context mContext = null;//上下文
    private SimpleAdapter gridAdapter;
    private GridView gridView;//显示选项


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_advanced);

        initParamsAndValues();

        initView();

        initData();
    }

    private void initParamsAndValues() {
        mContext = this;

        titles = getResources().getStringArray(R.array.index_titles);//标题
        mIconResourse = new int[]{R.drawable.tbsweb, R.drawable.fullscreen, R.drawable.filechooser,R.drawable.tbsweb};
        HashMap<String, Object> item = null;
        for (int i = 0; i < titles.length; i++) {
            item = new HashMap<>();
            item.put("title", titles[i]);
            item.put("icon", mIconResourse[i]);
            items.add(item);
        }
    }

    private void initView() {
        gridView = this.findViewById(R.id.item_grid);
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> gridView, View view,
                                    int position, long id) {
                Intent intent = null;
                switch (position) {
                    case FILE_CHOOSER: {
                        intent = new Intent(MainActivity.this, FilechooserActivity.class);
                        MainActivity.this.startActivity(intent);

                    }
                    break;
                    case FULL_SCREEN_VIDEO: {
                        intent = new Intent(MainActivity.this, FullScreenActivity.class);
                        MainActivity.this.startActivity(intent);
                    }
                    break;

                    case TBS_WEB: {
                        intent = new Intent(MainActivity.this, BrowserActivity.class);
                        MainActivity.this.startActivity(intent);
                    }
                    break;
                    case 3://获取地理位置
                        intent = new Intent(mContext,GetLocationActivity.class);
                        startActivity(intent);
                        break;
                }

            }
        });
    }

    private void initData() {
        this.gridAdapter = new SimpleAdapter(this, items,
                R.layout.function_block, new String[]{"title", "icon"},
                new int[]{R.id.Item_text, R.id.Item_bt});
        gridView.setAdapter(gridAdapter);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                this.tbsSuiteExit();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void tbsSuiteExit() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle("X5功能演示");
        dialog.setPositiveButton("OK", new AlertDialog.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Process.killProcess(Process.myPid());
            }
        });
        dialog.setMessage("quit now?");
        dialog.create().show();
    }
}
